<?php

namespace app\http\middleware;

use app\facade\Token;
use think\Db;
use app\models\{User as Users, Recpeople};
use app\facade\Redis;

class Pauth
{
    use \traits\controller\Jump;

    public function handle($request, \Closure $next)
    {
        if (!$request->header('token')) {
            $this->result(null, 999, '请登录用户');
        }
        if (!$request->header('peopleid')) {
            $this->result(null, 888, '未绑定师傅');
        }
        $user = $request->user;
        if (empty($user)) {
            $this->result(null, 999, '请登录用户');
        }
        $peopleid = $request->header('peopleid') ?? 0;
        if (empty($peopleid)) {
            $this->result([], 888, '未绑定师傅ID');
        }
        $people = (new Recpeople())->where('id', $peopleid)->find();
        if (empty($people)) {
            $this->result([], 888, '未绑定师傅信息');
        }
        if ($people['user_id'] != 0 && $people['user_id'] != $user['id']) {
            $this->result([], 888, '无登录权限');
        }
        $request->people = $people;
        return $next($request);
    }
}
